<template>
    <a-layout>
        <a-layout-sider width="200" style="background: #fff">
            <a-menu
                    v-model:selectedKeys="selectedKeys2"
                    v-model:openKeys="openKeys"
                    mode="inline"
                    :style="{ height: '100%', borderRight: 0 }"
            >
                <a-sub-menu key="sub1">
                    <template #title>
              <span>
                <user-outlined />
                商家管理
              </span>
                    </template>
                    <a-menu-item key="1">option1</a-menu-item>
                    <a-menu-item key="2">option2</a-menu-item>
                    <a-menu-item key="3">option3</a-menu-item>
                    <a-menu-item key="4">option4</a-menu-item>
                </a-sub-menu>
                <a-sub-menu key="sub2">
                    <template #title>
              <span>
                <laptop-outlined />
                商品管理
              </span>
                    </template>
                    <a-menu-item key="/admin/manager">
                        <router-link to="/admin/manager">品牌管理</router-link>
                        </a-menu-item>
                    <a-menu-item key="/admin/specification">
                        <router-link to="/admin/specification">规格管理</router-link>
                    </a-menu-item>
                    <a-menu-item key="/admin/typetemlate">
                        <router-link to="/admin/typetemlate">模板管理</router-link>
                    </a-menu-item>
                    <a-menu-item key="/admin/item">
                        <router-link to="/admin/item">分类管理</router-link>
                    </a-menu-item>
                    <a-menu-item key="9">
                        商品审核
                    </a-menu-item>
                </a-sub-menu>
                <a-sub-menu key="sub3">
                    <template #title>
              <span>
                <notification-outlined />
                广告管理
              </span>
                    </template>
                    <a-menu-item key="/admin/contentcate">
                        <router-link to="/admin/contentcate">广告类别</router-link>
                    </a-menu-item>
                    <a-menu-item key="/admin/content">
                        <router-link to="/admin/content">广告内容</router-link>
                    </a-menu-item>
                </a-sub-menu>
            </a-menu>
        </a-layout-sider>
        <a-layout style="padding: 0 24px 24px">
            <a-layout-content
                    :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
            >
                <p>
                    <a-button type="primary" @click="add">新建</a-button>
                </p>

                <a-table :dataSource="brandlist"
                         :columns="columns"
                         :pagination="pagination"
                         @change="handleTableChange"
                >
                    <template v-slot:action="{text,record}">
                        <a-button type="primary" @click="edit(record)">编辑</a-button>
                        <a-popconfirm
                                title="删除后无法恢复，确认删除吗?"
                                ok-text="是"
                                cancel-text="否"
                                @confirm="del(record.id)"
                        >
                            <a-button type="danger">删除</a-button>
                        </a-popconfirm>
                    </template>
                </a-table>
            </a-layout-content>
        </a-layout>
        <a-modal
                v-model:visible="modalVisible"
                title="商品规格编辑表单"
                @ok="handlModalOk"
                cancel-text="取消"
                ok-text="确定">

            <a-form :model="specEntity" :label-col="{span:6}" :wrapper-col="wrapperCol">
                <a-form-item label="规格名称">
                    <a-input v-model:value="specEntity.specification.specName" />
                </a-form-item>

                <a-form-item label="规格选项">
                    <a-button type="primary" :size="small" @click="addTableRow">新增规格选项</a-button>
                    <table id="tb01" width="340px" >
                        <thead>
                        <tr>
                            <td>规格选项</td>
                            <td>排序</td>
                            <td>操作</td>
                        </tr>
                        </thead>
                        <tbody>
                        <tr v-for="pojo in specEntity.specificationOptionList" v-bind:key="pojo.id">
                            <td><a-input v-model:value="pojo.optionName"/></td>
                            <td><a-input v-model:value="pojo.orders"/></td>
                            <td><a-button :size="small" danger block @click="deleTableRow($index)">删除</a-button></td>
                        </tr>
                        </tbody>
                    </table>
                </a-form-item>
            </a-form>
        </a-modal>
    </a-layout>
</template>

<script lang="ts">
    import {onMounted, ref} from "vue";
    import axios from "axios";
    import {message} from "ant-design-vue";

    export default {
        name: "specification",
        setup(){
            const brandlist =ref();
            const columns= [
                {
                    title: '规格id',
                    dataIndex: 'id',
                    width:'10%'
                },
                {
                    title: '规格名称',
                    dataIndex: 'specName',
                    width:'60%',
                    align:'center'
                },
                {
                    title: 'Action',
                    key: 'action',
                    width:'25%',
                    slots: {customRender:'action'}
                },
            ];

            const pagination=ref({
                current:1,
                pageSize:4,
                total:0,
            });


            const loading = ref(false);
            const modalVisible = ref<boolean>(false);
            const modalloading = ref<boolean>(false);
            const specifications = ref();
            const specEntity = ref<any>({
                specification:{},
                specificationOptionList:[]
            });
            //数据查询
            const handleQuery=(params:any)=>{
                axios.get('http://localhost:8899/brand-query/specification-Ms/listByPage',{
                    params:{
                        page:params.page,
                        size:params.size,
                    }
                }).then((response)=>{
                    loading.value=false;
                    brandlist.value=response.data.list;
                    //重置分页按钮
                    pagination.value.current=params.page;
                    pagination.value.total=response.data.total;
                });
            }
            //点中分页导航
            const handleTableChange=(pagination:any)=>{
                handleQuery({
                    page:pagination.current,
                    size:pagination.size
                })
            }

            //添加
            const add=()=>{
                modalVisible.value=true;
                specEntity.value={
                    specification:{},
                    specificationOptionList: []
                };
            }
            //删除
            const del=(id:number)=>{
                axios.delete('http://localhost:8899/brand-query/specification-Ms/delete/'+id).then(response=>{
                    if (response.data.code==200){
                        message.success('删除成功');
                        //加载最新数据
                        handleQuery({
                            page:pagination.value.current,
                            size:pagination.value.pageSize
                        });
                    } else {
                        message.error(response.data.message);
                    }
                })
            }
            //修改
            const edit=(id:number)=> {
                modalVisible.value = true;
                axios.get('http://localhost:8899/brand-query/specification-Ms/update').then(response => {
                    specEntity.value = response.data;
                })
            };

            //添加新行
            const addTableRow = ()=>{
                specEntity.value.specificationOptionList.push({});
            };

            //删除行
            const deleTableRow = (index:number) =>{
                specEntity.value.specificationOptionList.splice(index,1);
            };
            //修改
            const handlModalOk=()=>{
                modalloading.value=false;
                axios.post('http://localhost:8899/brand-query/specification-Ms/update',specEntity.value).then(response=>{
                    if (response.data.code==200){
                        modalVisible.value=false;
                        //加载最新数据
                        handleQuery({
                            page:pagination.value.current,
                            size:pagination.value.pageSize
                        });
                    } else  {
                        message.error(response.data.message);
                    }
                });
            }

            onMounted(()=>{
                handleQuery({
                    page:1,
                    size:pagination.value.pageSize,
                });
                //handleLoadNewsCategory();
            });

            // const handleLoadNewsCategory=()=>{
            //     axios.get('http://localhost:8899/brand-query/findOne').then(response=>{
            //         categoryList.value = response.data;
            //     })
            // }
            // onMounted(()=>{
            //     handleQuery({
            //         page:1,
            //         size:pagination.value.pageSize,
            //     });
            //     handleLoadNewsCategory();
            // });
            return{
                pagination,
                loading,
                modalVisible,
                modalloading,
                brandlist,
                columns,
                specifications,
                specEntity,
                handleQuery,
                handleTableChange,
                handlModalOk,
                add,
                edit,
                del,
                deleTableRow,
                addTableRow,
            }
        }
    }

</script>
